Cache
An advanced promise queue management library for JavaScript.
This library is designed to handle the concurrency of any asynchronous tasks encapsulated in promises, providing optional debouncing to manage the flow of promise execution.
- Limit the number of concurrent promises.
- Optional debouncing to manage the timing of promise execution.
- Ability to abort queued promises.
- Efficient and orderly management of asynchronous operations.
Installation
In Browser
Include assets
Simply download and include with a script tag, Queue
will be registered as a global variable.
<script src="../lib/queue.global.js"></script>
<script>
const queue = new Queue({ concurrency: 3, delay: 200 });
const asyncTask = function () {
return new Promise((resolve) => setTimeout(() => resolve("Task Complete"), 1000));
};
queue.add(asyncTask);
const asyncWithAbort = () => {
let timer;
const promise = new Promise((resolve) => {
timer = setTimeout(() => resolve("Another Task Complete"), 500);
});
const abortFunc = () => {
clearTimeout(timer);
console.log('Task was aborted');
};
return [promise, abortFunc]
}
queue.add(asyncWithAbort);
</script>
CDN
unpkg
<script src="https://unpkg.com/@nextgis/queue"></script>
jsdelivr
<script src="https://cdn.jsdelivr.net/npm/@nextgis/queue"></script>
We recommend linking to a specific version number /queue@[version]
In Node.js
npm install @nextgis/queue
Usage
import Queue from '@nextgis/queue';
const queue = new Queue({ concurrency: 2, delay: 100 });
const abortController = new AbortController();
queue.add(
() =>
fetch('https://api.example.com/data', {
signal: abortController.signal,
}),
abortController.abort,
);
setTimeout(() => {
queue.abort();
}, 100);
Check out the API Documentation
Commercial support
Need to fix a bug or add a feature to @nextgis/cache
? We provide custom development and support for this software. Contact us to discuss options!